/**
 * Created by Administrator on 2019/9/1.
 */



//1. 获得商品列表ul
var goods_container = document.querySelector("#goods-container");


//2. 冒泡事件获得 事件触发者
goods_container.onclick = function(event){
    var node = event.target;  //获得事件 触发者 元素对象 <input type="button" value="+">
    // 增加 + 或 减少 - onclick
    if(node.nodeName=="INPUT" && node.getAttribute("type") =="button"){
        if(node.value == "+"){  // <input type="button" value="+">
            var count = node.previousElementSibling.value ;

            node.previousElementSibling.value = ++count;
            console.log("数量:" + count);
            var unit_price = node.parentElement.previousElementSibling.lastElementChild.innerText;//单价
            console.log("单价:" + unit_price);

            node.parentElement.nextElementSibling.lastElementChild.innerText =  toFixed(count,unit_price);
            sumPrice();  //总价重新计算

        }else if(node.value == "-"){  //  <input type="button" value="-">
            var count = node.nextElementSibling.value ;
            if(count >=2) {

                node.nextElementSibling.value = --count;
                console.log("数量:" + count);
                var unit_price = node.parentElement.previousElementSibling.lastElementChild.innerText;//单价
                console.log("单价:" + unit_price);

                node.parentElement.nextElementSibling.lastElementChild.innerText = toFixed(count,unit_price);
                sumPrice();  //总价重新计算
            }
        }else {

        }

    }

    // 复选框 是否选中商品 - onclick
    if(node.nodeName=="INPUT" && node.getAttribute("type") =="checkbox"){
        sumPrice();
    }


}



//计算总价
function sumPrice(){

    var sum =0; //总价
    var total_number =0;  //总数量

    //1. 获得所有商品列表的 checkbox
    var ckbox =  document.querySelectorAll("#goods-container input[type='checkbox']");
    console.log(ckbox.length);
    for(var i =0;i<ckbox.length;i++){
        if(ckbox[i].checked){
            //小计价格
            var xiaoji = ckbox[i].parentElement.nextElementSibling.children[2].lastElementChild.innerText;
//            sum += parseInt(xiaoji);
            sum = accAdd(sum,parseFloat(xiaoji));

            //每个商品的数量
            var num = ckbox[i].parentElement.nextElementSibling.children[1].children[1].value;
            console.log("每个商品的数量：" + num);
            total_number += parseInt(num);


        }
    }
    console.log("总价：" + sum);
    document.querySelector(".sum-price").innerText = sum;
    document.querySelector(".goods-amount").innerText = total_number;

}

function selectAllTop(){
    var isSelectAllTop=document.querySelector("input[name='selectAll-top']").checked;
    var goods = document.querySelectorAll("input[name='good']");
    var goods_values ="";
    for (var i = 0; i < goods.length; i++) {
        goods[i].checked = isSelectAllTop;
        if (goods[i].checked)
            goods_values += goods[i].value + ",";
    }
    sumPrice();
    console.log("所选的车辆：" +goods_values );
}



function selectAllBottom(){
    var isSelectAllBottom=document.querySelector("input[name='selectAll-bottom']").checked;
    var goods = document.querySelectorAll("input[name='good']");
    var goods_values ="";
    for (var i = 0; i < goods.length; i++) {
        goods[i].checked = isSelectAllBottom;
        if (goods[i].checked)
            goods_values += goods[i].value + ",";
    }
    sumPrice();
    console.log("所选的车辆：" +goods_values );
}











/*浮点数精度缺失   乘法*/
function toFixed(arg1,arg2)
{
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

//浮点数精度缺失   加法
function accAdd(arg1,arg2){
    var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2))
    return (arg1*m+arg2*m)/m
}
